home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue68 / Alfresco / Comments.dpr < prev    next >
Encoding:
Text File  |  2001-02-16  |  4.2 KB  |  163 lines

  1. program Comments;
  2.  
  3. uses
  4.   SysUtils,
  5.   Classes,
  6.   AAChrStm;
  7.  
  8.  
  9. {$IFDEF DoNotCompile}
  10. //some constructions to test all cases
  11. {a braced comment}
  12. (*a parenstar comment*)
  13. /'{not a comment}'
  14. /any old chars
  15. /(*a comment after a slash*)
  16. /{a comment after a slash}
  17. ((*a comment after an open paren*)
  18. (//a comment after an open paren
  19. ({a comment after an open paren}
  20. (*a parenstar comment with a * inside of it*)
  21. {$ENDIF}
  22.  
  23.  
  24. procedure ExtractComments(aInStm  : TaaInCharStream;
  25.                           aOutStm : TaaOutCharStream);
  26. type
  27.   TStates = (          {the types of states...}
  28.     sScanNormal,       {..normal scanning}
  29.     sScanBraceComment, {..scanning comment after getting '{'}
  30.     sScanAfterSlash,   {..scanning after getting first '/'}
  31.     sScanSlashComment, {..scanning comment after getting '//'}
  32.     sScanAfterParen,   {..scanning after getting '('}
  33.     sScanPStarComment, {..scanning comment after getting '(*'}
  34.     sScanAfterStar,    {..scanning after getting '*' in (**) comment}
  35.     sScanString);      {..scanning a string}
  36. var
  37.   State : TStates;
  38.   Ch    : char;
  39. begin
  40.   State := sScanNormal;
  41.   Ch := aInStm.GetChar;
  42.   while (Ch <> #0) do begin
  43.     case State of
  44.       sScanNormal :
  45.         begin
  46.           case Ch of
  47.             '''': State := sScanString;
  48.             '(' : State := sScanAfterParen;
  49.             '/' : State := sScanAfterSlash;
  50.             '{' : begin
  51.                     aOutStm.PutChar('{');
  52.                     State := sScanBraceComment;
  53.                   end;
  54.           end;{case}
  55.         end;
  56.       sScanBraceComment :
  57.         begin
  58.           aOutStm.PutChar(Ch);
  59.           if (Ch = '}') then begin
  60.             aOutStm.PutChar(#10);
  61.             State := sScanNormal;
  62.           end;
  63.         end;
  64.       sScanAfterSlash :
  65.         begin
  66.           case Ch of
  67.             '''': State := sScanString;
  68.             '(' : State := sScanAfterParen;
  69.             '/' : begin
  70.                     aOutStm.PutChar('/');
  71.                     aOutStm.PutChar('/');
  72.                     State := sScanSlashComment;
  73.                   end;
  74.             '{' : begin
  75.                     aOutStm.PutChar('{');
  76.                     State := sScanBraceComment;
  77.                   end;
  78.           else
  79.             State := sScanNormal;
  80.           end;{case}
  81.         end;
  82.       sScanSlashComment :
  83.         begin
  84.           aOutStm.PutChar(Ch);
  85.           if (Ch = #10) then
  86.             State := sScanNormal;
  87.         end;
  88.       sScanAfterParen :
  89.         begin
  90.           case Ch of
  91.             '''': State := sScanString;
  92.             '(' : State := sScanAfterParen;
  93.             '*' : begin
  94.                     aOutStm.PutChar('(');
  95.                     aOutStm.PutChar('*');
  96.                     State := sScanPStarComment;
  97.                   end;
  98.             '/' : State := sScanAfterSlash;
  99.             '{' : begin
  100.                     aOutStm.PutChar('{');
  101.                     State := sScanBraceComment;
  102.                   end;
  103.           else
  104.             State := sScanNormal;
  105.           end;{case}
  106.         end;
  107.       sScanPStarComment :
  108.         begin
  109.           aOutStm.PutChar(Ch);
  110.           if (Ch = '*') then
  111.             State := sScanAfterStar;
  112.         end;
  113.       sScanAfterStar :
  114.         begin
  115.           aOutStm.PutChar(Ch);
  116.           if (Ch = ')') then begin
  117.             aOutStm.PutChar(#10);
  118.             State := sScanNormal
  119.           end
  120.           else
  121.             State := sScanPStarComment;
  122.         end;
  123.       sScanString :
  124.         begin
  125.           if (Ch = '''') then
  126.             State := sScanNormal;
  127.         end;
  128.     end;{case}
  129.     Ch := aInStm.GetChar;
  130.   end;
  131. end;
  132.  
  133.  
  134. var
  135.   InStm  : TFileStream;
  136.   OutStm : TFileStream;
  137.   InChStm  : TaaInCharStream;
  138.   OutChStm : TaaOutCharStream;
  139.   Ch : char;
  140.   SmallBuf : array [0..19] of byte;
  141. begin
  142.   InStm := nil;
  143.   InChStm := nil;
  144.   OutStm := nil;
  145.   OutChStm := nil;
  146.   try
  147.     InStm := TFileStream.Create('comments.dpr', fmOpenRead+fmShareDenyNone);
  148.     InChStm  := TaaInCharStream.Create(InStm);
  149.     OutStm := TFileStream.Create('comments.out', fmCreate);
  150.     OutChStm := TaaOutCharStream.Create(OutStm);
  151.  
  152.  
  153.     ExtractComments(InChStm, OutChStm);
  154.  
  155.   finally
  156.     OutChStm.Free;
  157.     OutStm.Free;
  158.     InChStm.Free;
  159.     InStm.Free;
  160.   end;
  161. end.
  162.  
  163.